﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Main.Master" AutoEventWireup="true" CodeBehind="ClientiFornitori.aspx.cs" Inherits="FimetContoDeposito.Anagrafiche.ClientiFornitori" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <asp:PlaceHolder ID="PlaceHolder1" runat="server">        
        <%: Scripts.Render("~/bundles/JQuery") %>          
        <%: Scripts.Render("~/bundles/JQueryUI") %>  
        <%: Styles.Render("~/bundles/Formly/css") %>
        <%: Scripts.Render("~/bundles/Formly") %>
    </asp:PlaceHolder>

    <!-- Common -->
    <script type="text/javascript" lang="javascript" src="../Scripts/Formly/FormlyHelper.js"></script>
    <script type="text/javascript" lang="javascript" src="../Scripts/GrayParrot.Core.js"></script> 
    <script type="text/javascript" lang="javascript" src="../Scripts/GrayParrot.AngularJS.Directives.Fileupload.js"></script> 
    <script type="text/javascript" lang="javascript" src="../Scripts/GrayParrot.AngularJS.Directives.DateTimePicker.js"></script> 
    <script type="text/javascript" lang="javascript" src="../Scripts/GrayParrot.AngularJS.js"></script> 
    <script type="text/javascript" lang="javascript" src="../Scripts/GrayParrot.Modals.js"></script> 
    <!-- Common -->

    <script type="text/javascript" lang="javascript" src="../Scripts/bootbox.min.js"></script> 

    <style>
        /*body {
         margin: 20px
        }

        .formly-field {
            margin-bottom: 0px !important; 
        }

        .formly.row{
            display:block;
            margin-right:10px;
            margin-left:10px;
        }

        .formly-field.section-label {
            margin-bottom: 0px
        }*/

        .bootbox {
            left: auto !important;
            width: auto !important;
            margin-left: auto !important;
            background-color: transparent !important;
            border: none !important;
            -webkit-box-shadow: none !important;
            box-shadow: none !important;
            position: relative !important;
            top: 50% !important;
            overflow: visible !important;
            transition: opacity 0.15s linear 0s !important;
        }
    </style>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <!-- 
        Sitemap Menu item:
        <siteMapNode url="~/ClientiFornitori.aspx" title="ClientiFornitori" description="ClientiFornitori" image="fa-laptop" id="ClientiFornitori"/>
    -->
    <section class="panel">
	    <header class="panel-heading">
			<b>Associazione Magazzini - Fornitori</b>
		</header>

		<div class="panel-body">

            <!-- PUT YOUR CONTENT HERE -->
            <div ng-app="formlyExample" ng-controller="MainCtrl as vm" name="divContainer" id="divContainer">
                <div ng-if="vm.loadingData.$$state.status === 0" style="margin:20px 0;font-size:2em">
                    <strong>Caricamento...</strong>
                </div>
                <div ng-if="vm.loadingData.$$status.state !== 0">
                    <form ng-submit="vm.onSubmit()" novalidate id="formlyForm">
                        <formly-form model="vm.model" fields="vm.fields" form="vm.form">
                            <%--<button type="sumbit" class="btn btn-primary submit-button" ng-disabled="vm.form.$invalid">Aggiungi</button>--%>
                        </formly-form>
                    </form>
                </div>
            </div>
		    <select multiple="multiple" id="my-select" name="my-select[]">
		    </select>
		</div>
        <footer class="panel-footer">
			<button type="button" class="btn" data-color="#F4AD41" onclick="Salva()">Salva</button>
            <button type="button" class="btn" onclick="CancellaAssociazione()">Cancella Associazione</button>
			<button type="button" class="btn" onclick="Annulla()">Cancella tutte le associazioni</button>
		</footer>
	</section>

</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="LocaJavascript" runat="server">
<script>
    $(document).ready(function () {
        var $select = $('#my-select').multiSelect({
            selectableHeader: "<div class='custom-header'>Magazzini</div>",
            selectionHeader: "<div class='custom-header'>Magazzini Associati</div>"
        });
    });
    
    var settings = new FormlySettings();
    settings.container = '#divContainer';
    settings.setLoad(settings.createGetConfig('/api/Fornitori/LoadAssociazioniFields'));

    FormlyManager(settings);

    function FornitoreChangeCallback() {
        var m_fornitore = $('#formly_1_asyncselect_Fornitori_0').val();

        var m_data = JSON.stringify({ Fornitore: m_fornitore });

        $.ajax({
            type: "POST",
            url: "/api/Fornitori/LoadAssociatedCustomers",
            data: m_data,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                $('#my-select').multiSelect('deselect_all');

                list = response.Clienti;
                if (list.length > 0) {
                    $.each(list, function (index, item) {
                        $('#my-select').multiSelect('addOption', { value: item.value, text: item.name });
                    });
                }

                var values = [];
                listA = response.Associati;
                if ((listA != null) && (listA.length > 0)) {
                    $.each(listA, function (index, item) {
                        values.push(item.value);
                    });
                }
                $('#my-select').multiSelect('select', values);

                $('#my-select').multiSelect('refresh');
            },
            error: function () {
                alert("Error");
            }
        });
    }

    function CancellaAssociazione() {

        var msg = '<center>Confermi la cancellazione dell\'associazione tra cliente e fornitore ?</center>';

        bootbox.confirm({
            title: "Associazione Clienti - Fornitori",
            message: msg,
            buttons: {
                cancel: {
                    label: '<i class="fa fa-times"></i> Cancel'
                },
                confirm: {
                    label: '<i class="fa fa-check"></i> Confirm'
                }
            },
            callback: function (result) {
                if (result) {
                    var m_fornitore = $('#formly_1_asyncselect_Fornitori_0').val();

                    var m_data = JSON.stringify({ Fornitore: m_fornitore });
                    $.ajax({
                        type: "POST",
                        url: "/api/Fornitori/CancellaAssociazioneClienti",
                        data: m_data,
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (response) {
                            alert(response);
                            $('#my-select').multiSelect('deselect_all');
                            $('#my-select').multiSelect('refresh');
                        },
                        error: function () {
                            alert("Error");
                        }
                    });
                }
            }
        });
    }

    function Annulla() {
        var msg = '<center>Confermi la cancellazione di tutte le associazioni del fornitore ?</center>';

        bootbox.confirm({
            title: "Associazione Clienti - Fornitori",
            message: msg,
            buttons: {
                cancel: {
                    label: '<i class="fa fa-times"></i> Cancel'
                },
                confirm: {
                    label: '<i class="fa fa-check"></i> Confirm'
                }
            },
            callback: function (result) {
                if (result) {
                    $('#my-select').multiSelect('deselect_all');
                }
            }
        });
    }

    function Salva() {
        var m_fornitore = $('#formly_1_asyncselect_Fornitori_0').val();
        var m_clienti_selezionati = $('#my-select').val();

        var m_data = JSON.stringify({ Fornitore: m_fornitore, Clienti: m_clienti_selezionati });
        $.ajax({
            type: "POST",
            url: "/api/Fornitori/SalvaAssociazioneClienti",
            data: m_data,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                alert(response);
                $('#my-select').multiSelect('refresh');
            },
            error: function () {
                alert("Si è verificato un errore.");
            }
        });
    }
</script>
</asp:Content>